﻿#include "Connection.h"
#include "TbInfo.h"
#include "DBInfo.h"
#include "Field.h"
#include "UserInfo.h"

std::shared_ptr<Connection> conn;

bool Test_Create()
{
	TBCreateInfo createInfo;
	createInfo.m_TableName = "User2";

	{
		Field field;
		field.SetName("Name");
		field.SetVarchar(11);
		createInfo.AddField(field);
	}

	{
		Field field;
		field.SetName("Password");
		field.SetVarchar(20);
		createInfo.AddField(field);
	}
	std::cout << createInfo.Sql();

	bool flag = conn->Execute(createInfo.Sql());
	return flag;

}

bool Test_Drop()
{

	TBDropInfo info2;
	info2.m_TableName = "User2";
	return conn->Execute(info2.Sql());

}
int main()
{
	Connection::Desc desc;
	desc.m_Database = "fxcc_db1";
	conn = std::make_shared<Connection>(desc);

	Test_Drop();

	TBCreateInfo createInfo;
	createInfo.m_TableName = "User2";


	createInfo.m_TableName = "User2";

	{
		Field field;
		field.SetName("Id");
		field.SetType(Field::Type::BigInt);
		field.m_AutoInc = true;
		field.m_NotNull = true;
		field.m_PrimaryKey = true;
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("Name");
		field.SetVarchar(11);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("Password");
		field.SetVarchar(20);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("Charge");
		field.SetDecimal(10, 5);
		field.SetDefaultFloat(0.025);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("CreateTime");
		field.SetType(Field::Type::DateTime);
		field.SetDefaultValue("CURRENT_TIMESTAMP");
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("CreateDate");
		field.SetType(Field::Type::Date);
		field.SetDefaultValue("(CURRENT_DATE)");
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("Type");
		field.SetType(Field::Type::SmallInt);
		field.SetDefaultInt(20);
		createInfo.AddField(field);
	}

	// 所有 MySQL 基础类型字段

	{
		Field field;
		field.SetName("TinyIntField");
		field.SetType(Field::Type::TinyInt);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("MediumIntField");
		field.SetType(Field::Type::MediumInt);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("IntField");
		field.SetType(Field::Type::Int);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("BigIntField");
		field.SetType(Field::Type::BigInt);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("FloatField");
		field.SetType(Field::Type::Float);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("DoubleField");
		field.SetType(Field::Type::Double);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("BooleanField");
		field.SetType(Field::Type::Boolean);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("CharField");
		field.SetChar(5);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("TinyTextField");
		field.SetType(Field::Type::TinyText);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("TextField");
		field.SetType(Field::Type::Text);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("MediumTextField");
		field.SetType(Field::Type::MediumText);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("LongTextField");
		field.SetType(Field::Type::LongText);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("BinaryField");
		field.SetType(Field::Type::Binary);
		field.m_DataLen = 8;
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("VarBinaryField");
		field.SetType(Field::Type::VarBinary);
		field.m_DataLen = 16;
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("TinyBlobField");
		field.SetType(Field::Type::TinyBlob);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("BlobField");
		field.SetType(Field::Type::Blob);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("MediumBlobField");
		field.SetType(Field::Type::MediumBlob);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("LongBlobField");
		field.SetType(Field::Type::LongBlob);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("TimeField");
		field.SetType(Field::Type::Time);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("TimestampField");
		field.SetType(Field::Type::Timestamp);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("YearField");
		field.SetType(Field::Type::Year);
		createInfo.AddField(field);
	}
	{
		Field field;
		field.SetName("JsonField");
		field.SetType(Field::Type::Json);
		createInfo.AddField(field);
	}
	
	bool flag = conn->Execute(createInfo.Sql());

	// Insert
	for (int i = 0; i < 100; i++)
	{
		std::string insertSql = R"(
insert into User2 (Name ,Password) values ('Fbx_1203', '968250')
)";
		conn->Execute(insertSql);
	}
	//  update

	std::string updateSql = R"(

update User2 set Name='Fbz_1209' 
)";
	conn->Execute(updateSql);

	DBDropInfo dbDropInfo;
	dbDropInfo.m_DatabaseName = "fxcc_test2";
	conn->Execute(dbDropInfo.Sql());

	DBCreateInfo dbCreateInfo;
	dbCreateInfo.m_DatabaseName = "fxcc_test2";
	conn->Execute(dbCreateInfo.Sql());

	std::vector<DatabaseInfo>  dbs;
	if (conn->ListDatabaseInfos(dbs))
	{
		for (const auto& db : dbs)
		{
			std::cout << db.m_Id << std::endl;
		}
	}


	{
		UserDeleteInfo user;
		user.m_Host = "%";
		user.m_User = "Fbz22";
		conn->Execute(user.Sql());
	}
	//{
	//	UserCreateInfo user;
	//	user.m_Host = "%";
	//	user.m_User = "Fbz";
	//	user.m_Password = "123456";
	//	conn->Execute(user.Sql());
	//}

	//std::vector<UserInfo> users;
	//if (conn->ListUsers(users))
	//{
	//	for (const auto& user : users)
	//	{
	//		std::cout << user.m_User << " " << user.m_Host << std::endl;
	//	}
	//}

	std::vector<TBInfo> tbInfos;
	if (conn->ListTables(tbInfos))
	{
		for (const auto& info : tbInfos)
		{
			std::cout << info.m_TableName << std::endl;
		}
	}
	TBGetInfo tbInfo;
	tbInfo.m_TableName = "user2";
	if (conn->GetTableInfo(tbInfo))
	{
		for (const auto& info : tbInfo.m_Fields)
		{
			std::cout
				<< " m_Name :" << info.m_Name
				<< " m_Type:" << info.GetTypeText()
				<< " m_NotNull :" << info.m_NotNull
				<< " m_Default :" << info.m_Default
				<< " m_AutoInc :" << info.m_AutoInc
				<< std::endl;
		}
	}
	return 0;


}
